﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using FutureOpen.Core;
using FutureOpen.Core.func;
using FutureOpen.Core.func.db;
public partial class MyJobTag_LoadView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string tag = Request.QueryString["tag"];        
        if (!string.IsNullOrEmpty(tag))
        {
            tag = HttpUtility.UrlDecode(tag);
            RenderPage(tag);
        }
    }
    private static readonly string fmtBody = "<div style=\"font-size:120%\">标签:&nbsp;{0}&nbsp;&nbsp;({1}篇文章,{2}人关注){3}</div><br />";
    private static readonly string fmtArts = "<a href=\"ArtView.aspx?ArtID={1}\">{0}</a><br />";
    private static readonly string fmtDate = "<br /><br />{0}-{1}-{2}<br />";
    private readonly string URL_FORMAT_NODEC = "<a href=\"#\" onClick =\"javascript:sendRequest('MyJobTag_LoadView.aspx?tag={1}&p={2}');\">{0}</a>";
    private readonly int psize = 20;

    public void RenderPage(string tag)
    {
        MyTagJob mtj = new MyTagJob();
        Tagjob tj = mtj.LoadTagByText(tag);
        StringBuilder sb = new StringBuilder();
        string artcount = mtj.GetArtCountByTag(tag).ToString();
        int iPage = 1;
        string pager = Request.QueryString["p"];
        if (pager != null) iPage = int.Parse(pager);
        int ePage = (int)Math.Ceiling( double.Parse(artcount) / psize);
        IList<Art> listarts = mtj.GetArtsByTag(tag, (iPage - 1) * psize, psize);

        string previousTime = string.Empty;
        for (int i = 0; i < listarts.Count;i++)
        {
            string date = listarts[i].Art_time.Date.ToString();
            if (previousTime != date)
            {
                previousTime = date;
                sb.Append(string.Format(fmtDate, listarts[i].Art_time.Year.ToString(), listarts[i].Art_time.Month.ToString(), listarts[i].Art_time.Day.ToString()));
            }
           
                sb.Append(string.Format(fmtArts, listarts[i].Art_title, listarts[i].Art_id.ToString()));
           
        }
        Response.Write(string.Format(fmtBody, tag, artcount, tj.Tagjob_count.ToString(), sb.ToString()));
        Response.Write(GetTagContext(iPage,ePage,tag));
        Response.Flush();
    }

    private string GetTagContext(int current, int end, string tag)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<br /><br />");
        bool vline = false;
        if (current > 1)
        {
            sb.Append(EncodeURL("<<&nbsp;前页", (current - 1).ToString(),tag));
            vline = true;
        }
    
        if (current < end)
        {
            if (vline) sb.Append("&nbsp;|&nbsp;");
            sb.Append(EncodeURL("后页&nbsp;>>", (current + 1).ToString(),tag));
        }
       
        sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;第&nbsp;[" + current + "/" + end + "]&nbsp;页");
        return sb.ToString();
    }

    private string EncodeURL(string title,string p, string tag)
    {
        return string.Format(URL_FORMAT_NODEC,title,tag, p);
    }
}
